﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace deckAnalyser.Support
{
  class Options
  {
    private static Dictionary<String, List<String>> options;

    static Options()
    {
      options = new Dictionary<String, List<String>>();

      DBWrapper.ExecuteNonQuery(
      "CREATE TABLE IF NOT EXISTS settings (" +
      "name text NOT NULL," +
      "value text NOT NULL" +
      ")"
      );

      DataTable dt = DBWrapper.GetDataTable("SELECT name,value FROM settings");

      foreach (DataRow row in dt.Rows)
      {
        String name = row["name"] as String;
        String value = row["value"] as String;

        Set(name, value);
      }
    }


    public static String Get(String name,String defaultValue)
    {
      if (options.Keys.Contains(name))
        return options[name].First();

      return defaultValue;
    }

    public static List<String> GetAll(String name)
    {
      if (options.Keys.Contains(name))
        return options[name];

      return new List<String>();
    }

    public static void Remove(String name)
    {
      if (options.Keys.Contains(name))
        options[name].Clear();
    }

    
    public static void Set(String name, String value)
    {
      if (!options.Keys.Contains(name))
        options[name] = new List<String>();

      options[name].Add(value);
    }

    public static void Save()
    {
      DBWrapper.ExecuteNonQuery("DELETE FROM settings");
      foreach (String name in options.Keys)
      {
        foreach(String value in options[name])
          DBWrapper.ExecuteNonQuery("INSERT INTO settings (name,value) VALUES ('" + name + "','" + value + "')");
      }
    }


  }
}
